@page "/prerendering-support"
@using Microsoft.AspNetCore.ProtectedBrowserStorage
@inject ProtectedLocalStorage ProtectedLocalStore

<h1>Prerendering support</h1>

<p>
    If you need to support prerendering while using browser-based storage, things are
    a little more complicated. It's impossible to access browser-based storage during
    prerendering, because there is no browser connection at that stage. So you need to avoid
    calling <code>GetAsync</code> during prerendering, and defer the loading operation until
    the client has connected back to the server.
</p>

<p>
    This component demonstrates how to work with prerendering. To test this out, change
    the value of <code>RenderMode</code> to <code>RenderMode.ServerPrerendered</code> inside <code>_Host.cshtml</code>,
    then restart your application. You <strong>must</strong> then navigate directly to
    this page's URL, otherwise you'll get an error, because the other pages don't support
    prerendering.
</p>

<p>
    Tip: In most cases, if you application is heavily built around use of browser-based storage,
    it's better not to enable prerendering, since it adds complexity and doesn't really offer
    any value, since you can't render a useful UI until the data has been loaded.
</p>

<fieldset>
    @if (currentCount.HasValue)
    {
        <p>Current count: <strong>@currentCount</strong></p>
        <button @onclick="@IncrementCount">Increment</button>
    }
    else
    {
        <p>Loading...</p>
    }
</fieldset>

@functions {
    int? currentCount;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await LoadStateAsync();
            StateHasChanged();
        }
    }

    async Task LoadStateAsync()
    {
        currentCount = await ProtectedLocalStore.GetAsync<int>("prerenderedCount");
    }

    async Task SaveStateAsync()
    {
        await ProtectedLocalStore.SetAsync("prerenderedCount", currentCount);
    }

    Task IncrementCount()
    {
        currentCount++;
        return SaveStateAsync();
    }
}
